package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.mapper.TbPayLogMapper;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.pojo.pay.TbPayLog;
import com.dongdongshop.service.PayLogServiceI;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Component
@Service
public class PayLogServiceImpl implements PayLogServiceI {
    @Resource
    private TbPayLogMapper payLogMapper;
    @Resource
    private TbOrderMapper orderMapper;

    @Override
    public TbPayLog selectByOutTradeNo(String messageBody) {
        return payLogMapper.selectByOutTradeNo(messageBody);
    }

    @Override
    @Transactional
    public void updatePayByOutTradeNo(TbPayLog payLog) {
        orderMapper.updateAlipayOrderByPayOrder(payLog.getOutTradeNo(),payLog.getTransactionId());
        payLogMapper.updateByPrimaryKeySelective(payLog);
    }

    @Override
    public void addPay(TbPayLog payLog) {
        System.out.println("进入方法");
        //根据payLogid查询Order表中的id
        List<TbOrder> orderList = orderMapper.selectByPayOrder(Long.parseLong(payLog.getOutTradeNo()));
        String str = "";
        for (TbOrder tbOrder : orderList) {
            str += tbOrder.getOrderId()+",";
        }
        payLog.setOrderList(str);
        System.out.println("payLog"+payLog);
        int count = payLogMapper.insertSelective(payLog);
        System.out.println("count"+count);
    }
}
